<?php
class SendmailController extends AppController{
    
    public function index()
    {
        if(!$_GET['u'])
            $this->redirect(array('controller' => 'products', 'action' => 'index'));
        $link = $_GET['u'];
        $link = base64_decode($link);
        if(substr($link,0,4) != 'http')
            $this->redirect(array('controller' => 'products', 'action' => 'index'));
        $this->set('link_product', $link);   
        
        $send_status = '';
        if ($this->request->is('post')) {
            $email_from = @$this->request->data['email_from'];
            $email_to = @$this->request->data['email_to'];
            $link_product = @$this->request->data['link_product'];
            //$subject = @$this->request->data['subject'];
            //$message = @$this->request->data['message'];
            
            $lang_code = $this->Session->read('lang_code');
            $this->loadModel('Category'); $this->Category->lang_code = $lang_code;
            $this->loadModel('Article'); $this->Article->lang_code = $lang_code;
            $category_id = $this->Category->get_category_id_by_code('gui-email-san-pham');
            $artiles = $this->Article->get_article_by_category_id($category_id, 0, 1);
            if(!empty($artiles))
            {
                $search = array('[email_from]', '[email_to]', '[link_product]');
                $replace = array($email_from, $email_to, $link_product);
                $content_email = $artiles['0']['article_desc']['description'];
                $content_email = str_replace($search, $replace, $content_email);
                $title_email = $artiles['0']['article_desc']['title'];
            }
            else
            {
                $content_email = $link_product;
                $title_email = 'Email giới thiệu sản phẩm';
            }

            $Email = new CakeEmail();
            $Email->config('smtp');
            $Email->emailFormat('html');
            $Email->to($email_to);
            $Email->subject($title_email);
            if ($Email->send($content_email) )
        	{
        		$send_status = "Gửi email cho bạn bè thành công.";
        	}else{
        		$send_status = "Gửi email cho bạn bè không thành công.";
        	}
        }
        $this->set('send_status', $send_status);
            
        $breadcrumbs[] = array('title' => 'Gửi sản phẩm cho bạn bè', 'link' => 'sendmail');
        $this->set('breadcrumbs', $breadcrumbs);
        
        
        $has_html = '';
        $this->set('has_html', $has_html);
        $header_menu_sidebar['title'] = 'Gửi sản phẩm cho bạn';
        $header_menu_sidebar['link'] = 'sendmail';
        $this->set('header_menu_sidebar', $header_menu_sidebar);
        $menu_sidebar = array();
        $this->set('menu_sidebar', $menu_sidebar);
        
    }
}